package com.box.controller;

import com.box.entity.DbAdmin;
import com.box.entity.DbTemplates;
import com.box.service.AdminService;
import com.box.service.TemplateService;
import com.box.utils.GlobalError;
import com.box.utils.LayuiResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/***
 *@author java214
 *@site www.java214.com
 *@company 一帆风顺网络科技有限公司
 *@create 2020-02-04 22:49
 *@ClassName 模板控制器
 *@Description TODO
 *@program MyBox
 *
 */
@Controller
@RequestMapping("/template")
public class TemplateController {

    private Logger logger=Logger.getLogger(TemplateController.class);

    @Autowired
    private AdminService adminService;

    @Autowired
    private TemplateService templatesService;


    @RequestMapping(value = "/templateList",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public LayuiResult getDataList(Integer page,Integer limit){

        return templatesService.getDataList(page,limit);
    }


    @RequestMapping(value = "/delTemplateById/{id}",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public LayuiResult deleteId(@PathVariable("id") Integer id){
        try{
            Integer n = templatesService.deleteById(id);
            if(n>0){
                return new LayuiResult(0,"删除成功");
            }
             return new LayuiResult(500,"删除失败");
        }catch (Exception e){

            return new LayuiResult(500,"删除失败");

        }
    }


    @RequestMapping(value = "/delTemplateAll/{templateStr}",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public LayuiResult deleteAllId(@PathVariable("templateStr") String templateStr){
        try{
            Integer n = templatesService.deleteByAll(templateStr);
            if(n>0){
                return new LayuiResult(0,"批量删除成功");
            }
            return new LayuiResult(500,"批量删除失败");
        }catch (Exception e){

            return new LayuiResult(500,"批量删除失败");

        }
    }


    @RequestMapping(value = "/insertTemplate",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public LayuiResult InsertTemplate(DbTemplates templates){
        try{
            Integer n = templatesService.insertTemplate(templates);
            if(n>0){
                return new LayuiResult(0,"添加成功");
            }
            return new LayuiResult(500,"添加失败");
        }catch (Exception e){

            return new LayuiResult(500,"添加失败");

        }
    }


    @RequestMapping(value = "/updateTemplate",method = {RequestMethod.POST,RequestMethod.GET})
    @ResponseBody
    public LayuiResult updateTemplate(DbTemplates templates){
        try{
            Integer n = templatesService.updateTemplate(templates);
            if(n>0){
                return new LayuiResult(0,"修改成功");
            }
            return new LayuiResult(500,"修改失败");
        }catch (Exception e){

            return new LayuiResult(500,"修改失败");

        }
    }

    /**
     * 页面跳转
     */
    @RequestMapping(value = "/page/templateList",method = {RequestMethod.POST,RequestMethod.GET})
    public String getDataList(Model model){

        try{
            DbAdmin admin= adminService.getDbAdminItems();
            model.addAttribute("admin", admin);
            return  "templates/templatesList";
        }catch (Exception e){

            model.addAttribute("error", GlobalError.ERROR);
            model.addAttribute("runtime", GlobalError.currentTime);
            model.addAttribute("info", GlobalError.INFO);
            return "error/404";
        }
    }

    @RequestMapping(value = "/page/setTemplate",method = {RequestMethod.POST,RequestMethod.GET})
    public String addPage(Model model){
        try{
            DbAdmin admin= adminService.getDbAdminItems();
            model.addAttribute("admin", admin);
            return  "templates/setTemplate";
        }catch (Exception e){

            model.addAttribute("error", GlobalError.ERROR);
            model.addAttribute("runtime", GlobalError.currentTime);
            model.addAttribute("info", GlobalError.INFO);
            return "error/404";
        }
    }

    @RequestMapping(value = "/page/editTemplate/{id}",method = {RequestMethod.POST,RequestMethod.GET})
    public String editPage(@PathVariable("id") Integer id, Model model){
        try{
            DbAdmin admin= adminService.getDbAdminItems();
            model.addAttribute("admin", admin);
            DbTemplates templates = templatesService.getOneById(id);
            logger.debug("查询到数据:"+templates);
            model.addAttribute("template",templates);
            return  "templates/editTemplate";
        }catch (Exception e){

            model.addAttribute("error", GlobalError.ERROR);
            model.addAttribute("runtime", GlobalError.currentTime);
            model.addAttribute("info", GlobalError.INFO);
            return "error/404";
        }
    }

}
